#include "myapp.h"
#include <QtDebug>
#include <QDir>
#include <QStringList>

MyApp::MyApp(int &argc, char **argv)
		: QApplication(argc, argv)
{
#ifdef NOSINGLEAPP
	alreadyRunning = false;
#else
	this->instance = new SingleApplication(QString("ImageHold Uploader"), this);
	alreadyRunning = instance->isRunning();
	if ( alreadyRunning )
	{
		for(int i = 1; i < argc; i++) //we don't need first arg as it is an app executable name
		{
			instance->sendMessage(QString::fromLocal8Bit(argv[i])); // as far as I can see this works on windows as well as on utf8 Linux
			qDebug() << QString::fromLocal8Bit(argv[i]);
		}
	}
#endif //NOSINGLEAPP
	// for QSettings
	setOrganizationName("Name_No");
	setOrganizationDomain("qt-apps.org");
	setApplicationName("ImageHold");
}
/*
MyApp::~MyApp()
{
	instance->deleteLater();
}
*/

QStringList MyApp::resourcesDirs()
{
	QStringList dirs;
#if defined(SHARED_DIR) //linux?
	dirs.append(SHARED_DIR);
#endif
	dirs.append(applicationDirPath());
	QString homeD = homeDir();
	if ( !homeD.isNull() )
		dirs.append(homeD);
	return dirs;
}

QString MyApp::homeDir()
{
#ifdef Q_WS_WIN
	QString home;
	QString imageholdDir = "ImageHold Uploader";
/*
	// Windows 9x
	if(QDir::homePath() == QDir::rootDirPath())
		home = ".";
	// Windows NT/2K/XP variant
	else
*/		home = QDir::homePath();

	// no trailing slash
	if(home.at(home.length()-1) == '/')
                home.truncate(home.length()-1);
#else
	QString home = QDir::homePath();
	QString imageholdDir = ".imagehold";
#endif
	QDir myHome(home + "/" + imageholdDir);
	if(!myHome.exists()) {
		QDir userHome(home);
		if ( userHome.mkdir(imageholdDir) )
			myHome.mkdir("plugins");
		else
			return QString::null;
	}

	return myHome.path();

}

QStringList MyApp::pluginsDirs()
{
	QStringList dirs;
	foreach(QString dir, resourcesDirs())
	{ dirs.append(dir.append("/plugins")); }
	return dirs;
}


